package com.itboye.hutouben.fragment;

import android.content.BroadcastReceiver;
import android.content.ClipData;
import android.content.ClipboardManager;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.net.Uri;
import android.view.View;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.itboye.hutouben.R;
import com.itboye.hutouben.activity.mysetting.PersonalActivity;
import com.itboye.hutouben.activity.mysetting.SettingActivity;
import com.itboye.hutouben.base.BaseFragment;
import com.itboye.hutouben.logincontroller.LoginController;
import com.itboye.hutouben.util.ByAlert;
import com.itboye.hutouben.util.Const;
import com.itboye.hutouben.util.IsUtilUid;
import com.itboye.hutouben.util.SPUtils;
import com.itboye.hutouben.util.UIAlertView02;
import com.itboye.hutouben.util.XImageLoader;
import com.itboye.hutouben.web.WebActivity;
import com.itboye.hutouben.widget.RoundImageView;

import static android.content.Context.CLIPBOARD_SERVICE;

/**
 * 我的
 */
public class MeFragment extends BaseFragment {

    TextView add_shap_title_tv;
    RoundImageView person_img;
    TextView user_name,user_code, myorder, weixiu, address, my_wallet, invate_tv, help_tv,xiaoxi,qiandao_tv;
    ImageView img_setting;
    RelativeLayout boda_phone;
    RelativeLayout top;
    TextView kefu_tel;//客服电话
    private UIAlertView02 uiAlertView;
    String yqm;

    //剪切板管理工具类
    private ClipboardManager mClipboardManager;
    //剪切板Data对象
    private ClipData mClipData;
    @Override
    public int initView() {
        return R.layout.fragment_person_data;
    }

    @Override
    public void onMyActivityCreated() {
        //add_shap_title_tv.setText("个人中心");
        mClipboardManager = (ClipboardManager) getActivity().getSystemService(CLIPBOARD_SERVICE);
        IntentFilter intentFilter = new IntentFilter(SettingActivity.EXITCHANGE);
        getActivity().registerReceiver(nichengBroadcastReceiver, intentFilter);
        IntentFilter nichengFilter = new IntentFilter(PersonalActivity.NICHENG);
        getActivity().registerReceiver(nicheng01BroadcastReceiver, nichengFilter);
        read();
    }
    //登陆或退出的广播
    BroadcastReceiver nichengBroadcastReceiver = new BroadcastReceiver() {
        public void onReceive(Context context, Intent intent) {
            read();
        }
    };
    //修改昵称或头像的广播
    BroadcastReceiver nicheng01BroadcastReceiver = new BroadcastReceiver() {
        public void onReceive(Context context, Intent intent) {
            read();
        }
    };
    @Override
    public void onDestroy() {
        super.onDestroy();
        getActivity().unregisterReceiver(nichengBroadcastReceiver);
        getActivity().unregisterReceiver(nicheng01BroadcastReceiver);
    }

    @Override
    public void onClick(View v) {
        Intent intent = null;
        switch (v.getId()) {
            case R.id.xiaoxi://消息
                LoginController.onXiaoXi(getActivity(), null);
                break;
            case R.id.qiandao_tv://签到记录
                LoginController.onQianDao(getActivity(), null);
                break;
            case R.id.person_img:
            case R.id.user_name:
            case R.id.top://个人信息
                LoginController.onPersonal(getActivity(), null);
                break;
            case R.id.weixiu://维修记录
                LoginController.onWeiXiuJiLu(getActivity(), null);
                break;
            case R.id.myorder://我的订单
                LoginController.onDingDan(getActivity(), null);
                break;
            case R.id.address://地址管理
                LoginController.onShouHuoDiZhi(getActivity(), null);
                break;
            case R.id.my_wallet://我的钱包
                LoginController.onQianBao(getActivity(), null);
                break;
            case R.id.invate_tv://邀请好友
                ByAlert.alert("功能开发中...");
                break;
            case R.id.img_setting://设置
                intent = new Intent(getActivity(), SettingActivity.class);
                startActivity(intent);
                break;
            case R.id.help_tv://帮助中心
                intent = new Intent(getActivity(), WebActivity.class);
                intent.putExtra("url", Const.helpUrl);
                intent.putExtra("title", "帮助中心");
                startActivity(intent);
                break;
            case R.id.boda_phone://拨打电话
                uiAlertView = new UIAlertView02(getActivity(), "温馨提示！", "是否拨打客服电话？", "是", "取消");
                uiAlertView.show();
                uiAlertView.setClicklistener(new UIAlertView02.ClickListenerInterface() {
                    @Override
                    public void doLeft() {
                        Intent intent = new Intent(Intent.ACTION_CALL, Uri.parse("tel:"
                                + "400-863-9156"));
                        startActivity(intent);
                        uiAlertView.dismiss();
                    }

                    @Override
                    public void doRight() {
                        uiAlertView.dismiss();
                    }
                });
                break;
        }

    }

    /**
     * 加入数据
     */
    private void read() {
        if ((boolean) SPUtils.get(getActivity(), null, Const.IS_LOGINED, false)) {
            String nikeName = (String) SPUtils.get(getActivity(), null,
                    Const.NICK, "");
            user_name.setText(nikeName);
            yqm = (String) SPUtils.get(getActivity(), null,
                    Const.YQM, "");
            user_code.setText("邀请码:"+yqm);
            String head = (String) SPUtils.get(getActivity(), null, Const.HEAD, "");
            if (head != null) {
                XImageLoader.load(Const.IMAGE_HEAD + IsUtilUid.isHead(),
                        person_img);
            } else {
                person_img.setImageResource(R.drawable.default_head);
            }

            user_code.setOnLongClickListener(new View.OnLongClickListener() {
                @Override
                public boolean onLongClick(View v) {
                    //创建一个新的文本clip对象
                    mClipData = ClipData.newPlainText("label", yqm);
                    //把clip对象放在剪贴板中
                    mClipboardManager.setPrimaryClip(mClipData);
                    //  mClipboardManager.setText(copy);
                    ByAlert.alert("复制成功！");
                    return false;
                }
            });

        } else {
            user_name.setText("立即登录");
            person_img.setImageResource(R.drawable.default_head);
            user_code.setText("");
        }
    }
}
